/**
 *
 * 升级界面
 *
 */
class UpgradePanel extends BasePanel {

    public constructor() {
        super();
    }
    private nameList: egret.TextField[];
    private valueList: egret.TextField[];
    private btnList: UpgradeBtn[];

    private bg: egret.Bitmap;
    private acceptBtn: EButton;
    private MyZhuBuildName: egret.TextField;
    private MyFuBuildName: egret.TextField;
    private hitOneName: egret.TextField;

	private _goldTxt: egret.TextField;
    private MyZhuBuildValue: egret.TextField;
    private MyFuBuildValue: egret.TextField;
    private hitOneValue: egret.TextField;
	private aniCount:number=0;
	
    // 初始化面板
    public initPanel(): void {
        this.bg = new egret.Bitmap();
        this.bg.texture = this.assets.getTexture("alertBg");
        this.bg.x = 60;
        this.bg.y = 274;
        var rect: egret.Rectangle = new egret.Rectangle(180,125,1,1);
        this.bg.scale9Grid = rect;
        this.bg.width = 359;
        this.bg.height = 340;
        this.addChild(this.bg);

        this.acceptBtn = new EButton(this,"acceptBtn",this.acceptBtnHandle);
        this.acceptBtn.x = this.bg.x + this.bg.width / 2 - this.acceptBtn.width / 2;
        this.acceptBtn.y = this.bg.y + this.bg.height - this.acceptBtn.height / 2 - 10;
        this.addChild(this.acceptBtn);

        this.nameList = [];
        this.valueList = [];
        this.btnList = [];

        /*==========nameList==========*/
        //MyZhuBuildName
        this.MyZhuBuildName = new egret.TextField();
        this.MyZhuBuildName.text = "主城HP:";
        this.nameList.push(this.MyZhuBuildName);
        //MyFuBuildName
        this.MyFuBuildName = new egret.TextField();
        this.MyFuBuildName.text = "箭塔攻击力:";
        this.nameList.push(this.MyFuBuildName);

        for(var i in SkillManager.i.list) {
            var skillName: egret.TextField = new egret.TextField();
            skillName.text = SkillManager.i.list[i].data.name + "等级:";
            this.nameList.push(skillName);
        }
        /*==========nameList==========*/

        /*==========valueList==========*/
        this.MyZhuBuildValue = new egret.TextField();
        this.valueList.push(this.MyZhuBuildValue);

        this.MyFuBuildValue = new egret.TextField();
        this.valueList.push(this.MyFuBuildValue);

        for(var i in SkillManager.i.list) {
            var skillValue: egret.TextField = new egret.TextField();
            this.valueList.push(skillValue);
        }
        /*==========valueList==========*/


        for(var i in this.nameList) {
            var skillName: egret.TextField = this.nameList[i];
            skillName.x = 100;
            skillName.y = 340 + 40 * i;
            skillName.size = 24;
            skillName.textColor = 0x373737;
            this.addChild(skillName);
        }
		this._goldTxt = new egret.TextField();
        this._goldTxt.x = this.nameList[this.nameList.length-1].x;
        this._goldTxt.y = this.nameList[this.nameList.length-1].y + 45;
		this.addChild(this._goldTxt);
        for(var i in this.valueList) {
            var skillValue: egret.TextField = this.valueList[i];
            skillValue.x = 260;
            skillValue.y = this.nameList[i].y - 1;
            skillValue.size = 24;
            skillValue.textColor = 0x373737;
            this.addChild(skillValue);
        }

        for(var j: number = 0;j < SkillManager.i.list.length + 2;j++) {
            var upgradeBtn: UpgradeBtn = new UpgradeBtn();
            upgradeBtn.x = this.valueList[j].x + this.valueList[j].width + 10;
            upgradeBtn.y = this.valueList[j].y - 11;
            upgradeBtn.touchEnabled = true;
            upgradeBtn.name = "upgradeBtn" + j;
            this.btnList.push(upgradeBtn);
            this.addChild(upgradeBtn);
        }
        this.addEvent();
        Global.dispatchEvent(Events.LevelUpgrade);
    }
    
    private acceptBtnHandle(): void {
        Global.dispatchEvent(MainNotify.closeUpgradePanelNotify);
        GameManager.i.GameResume();
    }

    private TOUCH_TAP_Handle(e: egret.TouchEvent): void {
        if(e.target instanceof UpgradeBtn) {
            if(e.target.name.indexOf("upgradeBtn") != -1) {
                if(e.target.name == "upgradeBtn0") {
                    if(BuildManage.i.MyZhuBuildList[0].data.consume < GameManager.i.gold) {
                        GameManager.i.gold -= BuildManage.i.MyZhuBuildList[0].data.consume;
                        BuildManage.i.MyZhuBuildList[0].data.level++;
                    } else {
                        this.goldAnimated();
                    }
                } else if(e.target.name == "upgradeBtn1") {
                    if(BuildManage.i.MyFuBuildList[0].data.consume < GameManager.i.gold) {
                        GameManager.i.gold -= BuildManage.i.MyFuBuildList[0].data.consume;
                        BuildManage.i.MyFuBuildList[0].data.level++;
                    } else {
                        this.goldAnimated();
                    }
                } else if(e.target.name == "upgradeBtn2") {
                    if(SkillManager.i.list[0].data.consume < GameManager.i.gold) {
                        GameManager.i.gold -= SkillManager.i.list[0].data.consume;
                        SkillManager.i.list[0].data.level++;
                    } else {
                        this.goldAnimated();
                    }
                } else if(e.target.name == "upgradeBtn3") {
                    if(SkillManager.i.list[1].data.consume < GameManager.i.gold) {
                        GameManager.i.gold -= SkillManager.i.list[1].data.consume;
                        SkillManager.i.list[1].data.level++;
                    } else {
                        this.goldAnimated();
                    }
                } else if(e.target.name == "upgradeBtn4") {
                    if(SkillManager.i.list[2].data.consume < GameManager.i.gold) {
                        GameManager.i.gold -= SkillManager.i.list[2].data.consume;
                        SkillManager.i.list[2].data.level++;
                    } else {
                        this.goldAnimated();
                    }
                }
                Global.dispatchEvent(Events.LevelUpgrade);
            }
        }
    }
    
    private levelUpgradeHandle(): void {
        this.MyZhuBuildValue.text = "" + BuildManage.i.MyZhuBuildList[0].data.bloodTotal;
        this.MyFuBuildValue.text = "" + BuildManage.i.MyFuBuildList[0].data.hitBlood;

        for(var i: number = 0;i < SkillManager.i.list.length;i++) {
            this.valueList[i + 2].text = "" + SkillManager.i.list[i].data.level;
        }
        for(var j: number = 0;j < this.btnList.length;j++) {
            this.btnList[j].x = this.valueList[j].x + this.valueList[j].width + 10;
            if(j == 0) {
                this.btnList[j].text = "" + BuildManage.i.MyZhuBuildList[0].data.consume;
            } else if(j == 1) {
                this.btnList[j].text = "" + BuildManage.i.MyFuBuildList[0].data.consume;
            } else {
                this.btnList[j].text = "" + SkillManager.i.list[j-2].data.consume;
            }
        }
    }
    
    private goldAnimated(): void {
        this.aniCount = 500;
        this._goldTxt.text = "金币不足";
        this._goldTxt.size = 18;
        this._goldTxt.textColor = 0xFF0000;
        this.addEventListener(egret.Event.ENTER_FRAME,this.enterFrame,this);
    }
    
    private enterFrame(): void {
        var ddd: number = 8;
        if(this.aniCount > 500 - ddd * 1) {
            this._goldTxt.textColor = 0xFFFFFF;
        } else if(this.aniCount > 500 - ddd * 2) {
            this._goldTxt.textColor = 0xFF0000;
        } else if(this.aniCount > 500 - ddd * 3) {
            this._goldTxt.textColor = 0xFFFFFF;
        } else if(this.aniCount > 500 - ddd * 4) {
            this._goldTxt.textColor = 0xFF0000;
        } else if(this.aniCount > 500 - ddd * 5) {
            this._goldTxt.textColor = 0xFFFFFF;
        } else if(this.aniCount > 500 - ddd * 6) {
            this._goldTxt.textColor = 0xFF0000;
        } else if(this.aniCount < 380) {
            this._goldTxt.text = "";
        }

        if(this.aniCount > 0) {
            this.aniCount--;
        }else if(this.aniCount <= 0) {
			this.removeEventListener(egret.Event.ENTER_FRAME,this.enterFrame,this);
		}
    }

    private addEvent(): void {
        this.addEventListener(egret.TouchEvent.TOUCH_TAP,this.TOUCH_TAP_Handle,this);
        Global.addEventListener(Events.LevelUpgrade,this.levelUpgradeHandle,this);
    }
    private removeEvent(): void {
        this.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.TOUCH_TAP_Handle,this);
        Global.removeEventListener(Events.LevelUpgrade,this.levelUpgradeHandle,this);
        this.removeEventListener(egret.Event.ENTER_FRAME,this.enterFrame,this);
    }

    public dispose(): void {
        this.removeEvent();
    }
}